package com.ychs.dpmms.controller;

import com.ychs.dpmms.entity.Developer;
import com.ychs.dpmms.entity.Report;
import com.ychs.dpmms.entity.ResultMessage;
import com.ychs.dpmms.entity.User;
import com.ychs.dpmms.service.IBaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * copyright(c)2021 YYB.ALL rights Reserved
 * <p>
 * 描述:
 *
 * @author 原玉波
 * @version 1.0
 * @date 2021/9/13
 */
@CrossOrigin
@RestController
@RequestMapping("/report")
public class ReportController {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource(name = "reportService")
    IBaseService<Report> service;
    @Resource(name = "developerService")
    IBaseService<Developer> developerService;

    //查询方法
    @GetMapping("/search")
    public ResultMessage search(Integer limit, Integer page, Integer developer,
                                Integer secondOrganization,Integer branch,Integer status) {
        Map<String, Object> conditions = new HashMap<>();
        logger.debug("enter report search");
        if (developer != null) {
            conditions.put("developer", developer);
        }
        if (status != null) {
            conditions.put("status", status);
        }
        if (secondOrganization != null) {
            Map<String, Object> developerConditions = new HashMap<>();
            developerConditions.put("secondOrganization", secondOrganization);
            developerConditions.put("branch", branch);
            List<Developer> developerList=developerService.search(developerConditions);
            logger.debug("developerListSize"+developerList.size());
            if (developerList.size()!=0) {
                conditions.put("developerList", developerList);
            }else{
                Developer developer1 = new Developer();
                developer1.setId(0);
                developerList.add(0, developer1);
                conditions.put("developerList", developerList);
            }
        }

        int count = service.searchTotalNumber(conditions);

        if (limit != null && page != null) {
            conditions.put("begin", (page - 1) * limit);
            conditions.put("pageSize", limit);
        }
        logger.debug("乐乐"+conditions.toString());
        List<Report> reportList = service.search(conditions);
        logger.debug("reportList=" + reportList.toString());
        ResultMessage resultMessage = new ResultMessage(true, reportList, "" + count);
        return resultMessage;
    }

    //添加方法
    @PostMapping("/add")
    public ResultMessage add(@RequestBody Report report) {
        logger.debug(report.toString());
        logger.debug("enter report add");
        String msg = "增加成功";
        boolean result = false;
        logger.debug("report=" + report);

        result = service.add(report);
        msg = result ? "增加成功" : "增加失败";
        logger.debug("user add result = " + result);
        ResultMessage resultMessage = new ResultMessage(result, report, msg);
        return resultMessage;
    }

    //添加方法
    @PostMapping("/modify")
    public ResultMessage modify(@RequestBody Report report) {
        logger.debug(report.toString());
        logger.debug("enter report modify");
        String msg = "更新成功";
        boolean result = false;
        logger.debug("report=" + report);

        result = service.modify(report);
        msg = result ? "更新成功" : "更新失败";
        logger.debug("report add result = " + result);
        ResultMessage resultMessage = new ResultMessage(result, report, msg);
        return resultMessage;
    }
}
